package org.iteam.xlz.manager.action.images;

import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.service.base.BaseParameterService;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.images.Images;
import org.iteam.xlz.core.service.images.ImagesService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/images/edit.html")})
public class ImagesAction extends AbstractModuleAction<Images> implements RestAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 563027117283924680L;
	
	@Override
	public Images getModel() {
		if(null==model){
			model=new Images();
		}
		return model;
	}
	
	@Override
	@Actions({@Action("/ad/screen/images/index"),@Action("/ad/main/images/index"),@Action("/product/info/images/index"),
		      @Action("/ad/terminal/images/index"),@Action("/activity/column/images/index"),
		      @Action("/membershipcard/charge/images/index"),@Action("/ticket/info/images/index"),
		      @Action("/activity/info/images/index")})
	public String index() throws SQLException {
		map.put("imgPath","");
		map.put("type",_id);
		if("product".equals(_id)){
			map.put("showAdd",true);
			map.put("otherSizes",BaseParameterService.getInstance().getValueByName("otherproductimagesize"));
			map.put("otherWidth",BaseParameterService.getInstance().getValueByName("otherproductimagewidth"));
			map.put("otherHeight",BaseParameterService.getInstance().getValueByName("otherproductimageheight"));
		}else{
			map.put("showAdd",false);
		}
		map.put("sizes",BaseParameterService.getInstance().getValueByName(_id+"imagesize"));
		map.put("width",BaseParameterService.getInstance().getValueByName(_id+"imagewidth"));
		map.put("height",BaseParameterService.getInstance().getValueByName(_id+"imageheight"));
		array=ImagesService.getInstance().findListByObjectId(model.getObjectid());
		super._id=model.getObjectid();
		return "INDEX";
	}

	@Override
	public String query() {
		return null;
	}

	@Override
	@Actions({@Action("/ad/screen/images/create"),@Action("/ad/main/images/create"),@Action("/product/info/images/create"),
		      @Action("/ad/terminal/images/create"),@Action("/activity/column/images/create"),@Action("/membershipcard/charge/images/create"),
		      @Action("/ticket/info/images/create"),@Action("/activity/info/images/create")})
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		if(StringUtils.isBlank(model.getImgaddr())){
			map.put("state",false);
			map.put("info","请上传图片");
		}else{
			String info=ImagesService.getInstance().createImages(model);
			map.put("state",null!=info);
			map.put("info",info);
		}
		return MAP;
	}

	@Override
	@Actions({@Action("/ad/screen/images/update"),@Action("/ad/main/images/update"),@Action("/product/info/images/update"),
		      @Action("/ad/terminal/images/update"),@Action("/activity/column/images/update"),
		      @Action("/membershipcard/charge/images/update"),@Action("/ticket/info/images/update"),
		      @Action("/activity/info/images/update")})
	public String update() {
		if(StringUtils.isBlank(model.getImgaddr())){
			map.put("state",false);
			map.put("info","请上传图片");
		}else{
			map.put("state",ImagesService.getInstance().updateImages(model));
		}
		return MAP;
	}

	@Override
	@Actions({@Action("/ad/screen/images/delete"),@Action("/ad/main/images/delete"),@Action("/product/info/images/delete"),
	      @Action("/ad/terminal/images/delete"),@Action("/activity/column/images/delete"),
	      @Action("/membershipcard/charge/images/delete"),@Action("/ticket/info/images/delete"),
	      @Action("/activity/info/images/delete")})
	public String delete() {
		map.put("state",ImagesService.getInstance().deleteImage(model));
		return MAP;
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public String editNew() {
		return null;
	}

	@Override
	public String show() {
		return null;
	}
}
